import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subject, takeUntil } from 'rxjs';
import { Store } from '@ngxs/store';
import { SaveAction, SaveSelector } from '../../store/save';
import * as _ from 'lodash';

@Component({
  selector: 'app-welcome',
  templateUrl: './welcome.component.html',
  styleUrls: ['./welcome.component.scss'],
})
export class WelcomeComponent implements OnInit, OnDestroy {
  data: any = {};
  private destroy: Subject<boolean> = new Subject<boolean>();
  temp: any;

  constructor(
      private store: Store,
  ) {
    this.store.select(SaveSelector.data).pipe(takeUntil(this.destroy)).subscribe(r => {
      console.log('r', r);
      this.temp = r;
      this.data = this.resolve(r);
      console.log('data', this.data);
    });
  }

  ngOnInit(): void {
    this.store.dispatch(new SaveAction.Load());
  }

  ngOnDestroy(): void {
    this.destroy.next(true);
    this.destroy.unsubscribe();
  }

  resolve(r: any) {
    let result: any = {
      '增加情报数量': 0,
      '增加灵炁数量': 0,
      '增加龙魂数量': 0,
      '增加天材地宝数量': 0,
      '增加攻击丹数量': 0,
      '增加防御丹数量': 0,
      '增加会心丹数量': 0,
      '增加体力丹数量': 0,
      '增加速度丹数量': 0,
      '增加洗点丹数量': 0,
      '增加改颜丹数量': 0,
      '增加塑形丹数量': 0,
      '增加玄族元神': 0,
      '增加仙族元神': 0,
      '增加魔族元神': 0,
      '增加妖族元神': 0,
      '增加鬼族元神': 0,
      '增加偃族元神': 0,
    };
    result['秘境情报'] = r['秘境情报'];
    result['灵炁数量'] = r['灵炁数量'];
    result['龙魂数量'] = _(r['玩家天材地宝']).filter(s => '龙魂' === s['名称']).size();
    result['天材地宝数量'] = _(r['玩家天材地宝']).filter(s => '天材地宝' === s['名称'] && 4 === s['品质']).size();
    result['攻击丹数量'] = _(r['玩家天材地宝']).filter(s => '攻击丹' === s['名称']).size();
    result['防御丹数量'] = _(r['玩家天材地宝']).filter(s => '防御丹' === s['名称']).size();
    result['会心丹数量'] = _(r['玩家天材地宝']).filter(s => '会心丹' === s['名称']).size();
    result['体力丹数量'] = _(r['玩家天材地宝']).filter(s => '体力丹' === s['名称']).size();
    result['速度丹数量'] = _(r['玩家天材地宝']).filter(s => '速度丹' === s['名称']).size();
    result['洗点丹数量'] = _(r['玩家天材地宝']).filter(s => '洗点丹' === s['名称']).size();
    result['改颜丹数量'] = _(r['玩家天材地宝']).filter(s => '改颜丹' === s['名称']).size();
    result['塑形丹数量'] = _(r['玩家天材地宝']).filter(s => '塑形丹' === s['名称']).size();
    result['玄族元神数量'] = _(r['神识链表']).find(s => s['品质'] === 6 && s['种族'] === 1)?.['数量'];
    result['仙族元神数量'] = _(r['神识链表']).find(s => s['品质'] === 6 && s['种族'] === 2)?.['数量'];
    result['魔族元神数量'] = _(r['神识链表']).find(s => s['品质'] === 6 && s['种族'] === 3)?.['数量'];
    result['妖族元神数量'] = _(r['神识链表']).find(s => s['品质'] === 6 && s['种族'] === 4)?.['数量'];
    result['鬼族元神数量'] = _(r['神识链表']).find(s => s['品质'] === 6 && s['种族'] === 5)?.['数量'];
    result['偃族元神数量'] = _(r['神识链表']).find(s => s['品质'] === 6 && s['种族'] === 6)?.['数量'];
    return result;
  }

  submit() {
    let actions = [];

    if (this.data['增加情报数量'] > 0) {
      actions.push(new SaveAction.ChangeIntelligenceQuantity(this.data['增加情报数量']));
    }
    if (this.data['增加灵炁数量'] > 0) {
      actions.push(new SaveAction.ChangeMoney(this.data['增加灵炁数量']));
    }
    if (this.data['增加龙魂数量'] > 0) {
      actions.push(new SaveAction.AddDragonSoul(this.data['增加龙魂数量']));
    }
    if (this.data['增加天材地宝数量'] > 0) {
      actions.push(new SaveAction.AddTreasure(this.data['增加天材地宝数量']));
    }
    if (this.data['增加攻击丹数量'] > 0) {
      actions.push(new SaveAction.AddAttackMedical(this.data['增加攻击丹数量']));
    }
    if (this.data['增加防御丹数量'] > 0) {
      actions.push(new SaveAction.AddDefenseMedical(this.data['增加防御丹数量']));
    }
    if (this.data['增加会心丹数量'] > 0) {
      actions.push(new SaveAction.AddCriticalMedical(this.data['增加会心丹数量']));
    }
    if (this.data['增加体力丹数量'] > 0) {
      actions.push(new SaveAction.AddHealthMedical(this.data['增加体力丹数量']));
    }
    if (this.data['增加速度丹数量'] > 0) {
      actions.push(new SaveAction.AddSpeedMedical(this.data['增加速度丹数量']));
    }
    if (this.data['增加洗点丹数量'] > 0) {
      actions.push(new SaveAction.AddCleanMedical(this.data['增加洗点丹数量']));
    }
    if (this.data['增加改颜丹数量'] > 0) {
      actions.push(new SaveAction.AddFaceMedical(this.data['增加改颜丹数量']));
    }
    if (this.data['增加塑形丹数量'] > 0) {
      actions.push(new SaveAction.AddBodyMedical(this.data['增加塑形丹数量']));
    }

    if (this.data['增加玄族元神'] > 0) {
      actions.push(new SaveAction.AddSoul1(this.data['增加玄族元神']));
    }
    if (this.data['增加仙族元神'] > 0) {
      actions.push(new SaveAction.AddSoul2(this.data['增加仙族元神']));
    }
    if (this.data['增加魔族元神'] > 0) {
      actions.push(new SaveAction.AddSoul3(this.data['增加魔族元神']));
    }
    if (this.data['增加妖族元神'] > 0) {
      actions.push(new SaveAction.AddSoul4(this.data['增加妖族元神']));
    }
    if (this.data['增加鬼族元神'] > 0) {
      actions.push(new SaveAction.AddSoul5(this.data['增加鬼族元神']));
    }
    if (this.data['增加偃族元神'] > 0) {
      actions.push(new SaveAction.AddSoul6(this.data['增加偃族元神']));
    }

    if (actions.length > 0) {
      this.store.dispatch([...actions]).subscribe(() => {
        this.store.dispatch(new SaveAction.Save());
      });
    }

  }
}
